<template>
  <div class="vip--user-list views-container">
    <export-file-in ref="exportFileIn" :config="{title:'导入会员信息', desinfo:'请先导出会员数据, 填好信息, 再导入文件即可。',api: exportFileInApi}" :changeEvt="filesSerch">
      <el-button slot="export" size="mini" type="text" v-if="isIndependent" v-exceldown="{path:'web/doc/会员导入.csv', isBase: false}">下载会员模板</el-button>
      <el-button slot="export" size="mini" type="text" v-if="!isIndependent" v-exceldown="{path:'addons/weliam_areashop/web/doc/会员导入.csv', isBase: false}">下载会员模板</el-button>
    </export-file-in>
    <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
      <div class="wlm-form" v-if="dialogTableVisible">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">{{`${!!formFormatData.api['editForm']['params']['id']?'编辑':'添加'}会员`}}</span>
        </div>
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <div class="wlm-form-content">
            <div class="wlm-dialog-main">
              <div class="wlm-dialog-content">
                <el-form ref="user" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="120px" class="retail-form" label-position="right">
                  <el-form-item label="用户">
                    <chooseUser :config="initChooseUser" ref="chooseUser" :changeEvt="userChange"></chooseUser>
                  </el-form-item>
                  <el-form-item label="会员卡名称">
                    <el-select filterable v-model="formFormatData.formData.card_id" placeholder="请选择">
                      <el-option v-for="item in categoryData" :key="item.card_id" :label="item.card_title" :value="item.card_id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
        <el-button size="small" @click="formFormatSubmit" type="primary">保存</el-button>
      </footer>
    </el-dialog>
    <div class="wlm-table">
      <div class="wlm-table-hearder-btn">
        <el-button size="small" @click="dialogTableVisible = true;initChooseUser.closable = true;" type="primary">添加会员</el-button>
      </div>
      <div class="wlm-table-header">
        <el-form :model="tableFormatData.userTable.files" size="small" label-width="95px" class="retail-form" label-position="right">
          <el-form-item label="" class="filter">
            <el-form-item label="会员：">
              <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.title" placeholder="请输入会员昵称或id" style="width:160px;"></el-input>
            </el-form-item>
            <el-form-item label="会员卡号：">
              <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.card_number" placeholder="请输入会员卡号" style="width:160px;"></el-input>
            </el-form-item>
            <el-form-item label="开卡方式：">
              <el-select v-model="tableFormatData.userTable.files.card_mode" placeholder="请选择" style="width:200px;">
                <el-option label="全部" value="">
                </el-option>
                <el-option label="平台添加" :value="1">
                </el-option>
                <el-option label="用户购买" :value="2">
                </el-option>
                <el-option label="激活码兑换" :value="3">
                </el-option>
                <el-option label="购买商品" :value="4">
                </el-option>
                <el-option label="商品加购卡" :value="5">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form-item>
          <el-form-item label="" class="filter-btns">
            <el-button type="primary" @click="filesSerch">筛选</el-button>
            <el-button plain @click="$refs.exportFileIn.toggle()">导入会员</el-button>
            <el-button plain icon="document" :data-query="JSON.stringify(tableFormatData.userTable.files)" v-exceldown="{path:'outputMember&',query: {}}">{{$t('excel.export') }}会员</el-button>
            <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="wlm-table-content">
        <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
          <!-- <el-table-column type="selection" width="55">
                </el-table-column> -->
          <el-table-column prop="date" label="会员" align="left" min-width="100">
            <template slot-scope="scope">
              <div class="wlm-table-logos" v-if="scope.row.user">
                <img class="logo" :src="scope.row.user.avatarUrl">
                <div class="inner-text">
                  <p>{{scope.row.user.nickName}}</p>
                  <span>ID:{{scope.row.user_id}}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="会员卡号">
            <template slot-scope="scope">
              <span>{{scope.row.card_number}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="电话">
            <template slot-scope="scope">
              <span>{{scope.row.mobile||'-'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="开卡方式">
            <template slot-scope="scope">
              <p v-if="scope.row.card_mode=='1'">平台添加</p>
              <p v-if="scope.row.card_mode=='2'">用户购买</p>
              <p v-if="scope.row.card_mode=='3'">激活码兑换</p>
              <p v-if="scope.row.card_mode=='4'">购买商品</p>
              <p v-if="scope.row.card_mode=='5'">商品加购卡</p>
            </template>
          </el-table-column>
          <el-table-column label="到期时间">
            <template slot-scope="scope">
              <span>{{Number(scope.row.card_end_time)*1000 | parseTime('{y}-{m}-{d}')}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="消费次数">
            <template slot-scope="scope">
              <span>{{scope.row.card_buy_times}}</span>
            </template>
          </el-table-column>
          <el-table-column label="消费金额">
            <template slot-scope="scope">
              <p><span class="orders-red">￥</span><span>{{scope.row.card_money}}</span></p>
            </template>
          </el-table-column>
          <el-table-column label="成为时间">
            <template slot-scope="scope">
              <span>{{scope.row.create_time}}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户状态">
            <template slot-scope="scope">
              <p v-if="scope.row.card_cancel==1">已禁用</p>
              <p v-if="Date.now()<(scope.row.card_end_time*1000)&&scope.row.card_cancel==2">正常</p>
              <p v-if="Date.now()>(scope.row.card_end_time*1000)&&scope.row.card_cancel==2">已过期</p>
            </template>
          </el-table-column>
          <!-- <el-table-column label="是否禁用" >
                    <template slot-scope="scope">
                        <el-switch :active-value="1" :inactive-value="2" @change="changeTableItem({id:scope.row.card_user_id,card_cancel:scope.row.card_cancel},tableFormatData.userTable.change.card_cancel,{isWarning: scope.row.card_cancel === 1,warning:'会员一经禁用不可恢复，禁用后所产生的纠纷由服务商自身承担 如需禁用请慎重操作',cancel:{msg:'已取消禁用',val:{scope:scope,cancelStatus:{key:'card_cancel',val:2}}}})" v-model="scope.row.card_cancel"> </el-switch>
                    </template>
                </el-table-column> -->
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div class="operation-group">
                <router-link class="wlm-text" tag="span" :to="{path:'/user/managementUserEdit', query :{ user_id:scope.row.user_id}}">
                  <el-button type="text">查看</el-button>
                </router-link>
                <div class="btn-line"></div>
                <el-button class="wlm-text" type="text" @click="scope.row.card_cancel = scope.row.card_cancel === 1 ? 2 : 1;changeTableItem({id:scope.row.card_user_id,card_cancel:scope.row.card_cancel},tableFormatData.userTable.change.card_cancel,{isWarning: scope.row.card_cancel === 1,warning:'是否确认禁用会员？',cancel:{msg:'已取消禁用',val:{scope:scope,cancelStatus:{key:'card_cancel',val:2}}}})">{{scope.row.card_cancel === 1 ? '解除禁用':'禁用'}}</el-button>
                 <div class="btn-line"></div>
                  <el-button class="wlm-text" type="text" @click="delbtn(scope.row.card_user_id)">删除</el-button>
              </div>
            </template>

          </el-table-column>
        </el-table>
        <div class="pagination-content flex-row flex-justify-e flex-align-c">
          <!-- <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row> -->
          <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getMember,
  importMember,
  memberUserAddEdit,
  selectedCard,
  getDataMemberFind,
  changeCancel,
  destroyMember,
  destroyMemberUser,
} from '@/api/application'
import mixins from '@/mixins/mixins'
import ExportFileIn from '@/components/ExportFileIn/index'
import ChooseUser from '@/components/ChooseUser/index'
import exceldown from '@/directive/exceldown'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'VipUserList',
  components: {
    ChooseUser,
    ExportFileIn
  },
  directives: {
    exceldown
  },
  computed: {
    isIndependent() {
      return this.$store.getters.independent
    }
  },
  created() {
    this.$nextTick(() => {
      selectedCard().then((response) => {
        const {
          data: {
            data: msgData = []
          }
        } = response
        this.categoryData = msgData
      })
    })
  },
  data() {
    return {
      exportFileInApi: importMember,
      dialogTableVisible: false,
      selecStoreData: [],
      categoryData: [],
      blogTitle: '',
      initChooseUser: {
        closable: true,
        initList: []
      },
      formFormatData: {
        key: 'user',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            api: getDataMemberFind,
            params: {
              id: ''
            }
          },
          submitForm: memberUserAddEdit
        },
        formData: {
          card_id: '',
          card_mode: '1',
          mid: ''
        },
        rules: {
          // password: [{
          //   required: true,
          //   message: '请输入核销密码',
          //   trigger: 'blur'
          // }]
        }
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          model: {
            bindDialog: 'dialogTableVisible'
          },
          api: {
            getList: getMember,
            delList: destroyMember
          },
          tableData: [],
          files: {
            card_number: '',
            card_mode: '',
            title: '',
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          change: {
            card_cancel: {
              key: 'card_cancel',
              api: changeCancel
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    formFormatCallBack(data, type, response) {
      if (type === 'editForm') {
        const { data: { data: { user_id, user: { nickName = '' } } } } = response
        this.initChooseUser.initList = [{ user_id, nickName }]
        this.formFormatData.formData.mid = user_id
      }
    },
    saveDialogForm() {
      this.initChooseUser.initList = []
      this.resetDialogFormData({ card_id: '', mid: '', card_mode: '1' })
    },
    // user id 给formdata里面的mid提交
    userChange(userlist) {
      const [{
        user_id
      }] = [...userlist]
      this.formFormatData.formData.mid = user_id
    },
    delbtn(card_user_id){
        this.$confirm('请勿随意删除会员用户，删除后数据将不能恢复，一切后果自行承担！！！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           destroyMemberUser({card_user_id:card_user_id}).then(res=>{
                         if(res.data.code==1){
                             this.$message.success(res.data.msg)
                             this.filesSerch()
                         }
             })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  }
}
</script>
